home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4153 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.4 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: novice question on copy constru
  5. Date: 28 Jan 1996 14:17:50 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4eg0ie$ee@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Jan 28, 1996 12:18:22 in article <novice question on copy constru>,
  15. 'ee_ckmaa@uxmail.ust.hk (Chan Ka Ming)' wrote: 
  16.  
  17.  
  18. >I don't understand why one should create a copy constructor. Doesn't the 
  19. >computer will do the job for you when pass arguments by value? Thanks 
  20. Yes, the compiler generates a copy constructor for you, but with 
  21. default behavior.  Often this default causes disaster.  To be 
  22. certain that the copy constructor behaves as intended, 
  23. one must write a custom copy constructor. 
  24.  
  25. For example, suppose you have a class that contains a pointer to 
  26. an object, and your destructor frees the memory for that object. 
  27. The default copy constructor copies the pointer to the temporary 
  28. object and, when the temporary is destroyed, it deletes the 
  29. object pointed to.  This leaves the original instance of your  
  30. class pointing to a delete'd memory.  You can imagine what 
  31. will happen when the destructor for the original object is invoked. 
  32.  
  33. -- 
  34. Pete Grant 
  35. Kalevi, Inc. 
  36. Object Oriented Software Development
  37.